import { SelectQueryBuilder } from 'typeorm';
// tslint:disable:max-line-length
// tslint:disable:triple-equals
export class TypeormBuilder {
    public static addNumberEquals(condition: Array<{ key: string, value: any }>, tablename: string, query: SelectQueryBuilder<any>) {
        condition.forEach((element) => {
            if (element.value != null && element.value !== '') {
                query.andWhere(tablename + '.' + element.key + ' = ' + element.value);
            }
        });
        return query;
    }

    public static addStringEquals(condition: Array<{ key: string, value: any }>, tablename: string, query: SelectQueryBuilder<any>) {
        condition.forEach((element) => {
            if (element.value != null && element.value !== '') {
                query.andWhere(tablename + '.' + element.key + ' = \'' + element.value + '\'');
            }
        });
        return query;
    }

    public static addLikes(condition: Array<{ key: string, value: any }>, tablename: string, query: SelectQueryBuilder<any>) {
        condition.forEach((element) => {
            if (element.value != null && element.value !== '') {
                query.andWhere(tablename + '.' + element.key + ' LIKE ' + '\'%' + element.value + '%\'');
            }
        });
        return query;
    }

    public static addSubQuery(condition: any, value: any, query: SelectQueryBuilder<any>) {
        if (value != null && value !== '') {
            query.andWhere(condition);
        }
        return query;
    }
}
